import dd from 'gdt-jsapi';
import request from '../utils/request';
Page({
  data: {
    deptloading:false,
    toastVisable:false,
    basicTwoVisible:false,
    pickerVisible: true,
    defaultValue: '',
    defaultObj:{},
    list: [],
    checkedItem:null,
    userInfo:null,
    deptList: [],//当前选中账号的可选单位
    options_deptList:[]//筛选单位
  },
  getDDAuthCode(){
    dd.getAuthCode({
      corpId: "" 
    }).then(res => {
        this.setData({
          auth_code: res.code
        });   
        
        // console.log(res.code)
        // console.log(this)
        if(res.code){
          this.getLoginUserListByAuthCode(res.code); 
        }else{
          this.data.toastVisable = true;
        }                      
    }).catch(err => {})
  },
  getLoginUserListByAuthCode(authCode){
    // console.log("authCode"+authCode)
    dd.httpRequest({
      url: request.ipAddress +'/api/system/getLoginUserListByAuthCode',
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      data : JSON.stringify({
        "auth_code":  authCode
      }),
      dataType: 'json'}).then(res => {
        console.log(res)
        console.log('success');
        console.log(res);
        this.setData({
          list : res.data.list
        });

        if(res.data.list.length>0){
          this.defaultValue = res.data.list[0].value;
          this.defaultObj = res.data.list[0];
        }
      }).catch(err => {});
  },
  handleOk(value, column, e) {
    this.setData({
      basicTwoVisible: true,
    });
    // console.log('onOk value', value, 'onOk  column', column, e);
    this.setData({userInfo:column});
    this.getDeptMenu(column);         
  },
  onChangeCheck(v, items, e) {
    // console.log('当前选中的值为：', v, items, e);
    this.setData({checkedItem:items});
  },
  //根据输入对单位进行filter
  onChange(value, e) {    
    // console.log(value, e);
    let midList = this.data.deptList.filter(item => item.title.includes(value));
    this.setData({
      options_deptList :  midList
    });
  },
  //获取单位信息
  getDeptMenu(userInfo){
    let params = {
      "username":  userInfo.username,
      "password":  userInfo.password,
      "captcha":  null,
      "phone":  null,
      "zzdPasswordFlag":"1"
    };
    this.setData({ deptList :  [] });
    this.setData({ options_deptList :  [] });
    this.setData({ deptloading :  true});
    dd.httpRequest({
      url: request.ipAddress +'/api/system/login',
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      data : JSON.stringify(params),
      dataType: 'json'
    }).then(res => {
        this.setData({ deptloading :  false});
        let list = res.data.pageData.list;
        list.map(item => {
          // 为每个对象添加 value 字段，其值为 orgId
          item.value = item.orgId;
          return item;
        });
        this.setData({
          deptList :  list
        });
        this.setData({
          options_deptList :  list
        });
      }).catch(err => {});
  },
  handleClose() {
    this.setData({
      basicTwoVisible: false,
    });
  },
  handlePrimaryButtonTap() {
    this.handleClose();
    // my.showToast({ content: '点击主按钮', duration: 1000 });
    // console.log(this.data.checkedItem);
    if(this.data.checkedItem!=null){
      this.data.checkedItem.username  = this.data.userInfo.username;
      this.data.checkedItem.password  = this.data.userInfo.password;
      
      dd.navigateTo({
        url: '../statictis/statictis?loginInfo='+JSON.stringify(this.data.checkedItem) 
      })
    }   
  },
  handleSecondaryButtonTap() {
    this.handleClose();
    // my.showToast({ content: '点击辅助按钮', duration: 1000 });
    this.setData({checkedItem:null});
  },
  onLoad(query) {
    // 页面加载
    //AuthCode只能猜手机上调试调用获取
    // this.getDDAuthCode();  
  }, 
  onReady() {
    // 页面加载完成
    // this.getLoginUserListByAuthCode(); 
  },
  onShow() {
    // this.pickerVisible = true;
    // 页面显示
    this.getDDAuthCode();  
    // this.getLoginUserListByAuthCode(); 
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});